package vip.xiaonuo.hontu.listener.handle.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.hontu.listener.HongTuActivePushDataPublisher;
import vip.xiaonuo.modular.htpushdata.entity.HtPushData;
import vip.xiaonuo.modular.htpushdata.service.HtPushDataService;
import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class HtHandleDataTimer implements TimerTaskRunner {

    @Resource
    private HtPushDataService service;

    @Resource
    private HongTuActivePushDataPublisher publisher;

    /**
     * 鸿图数据的兜底程序
     * 1分钟执行一次
     */
    private void htPushDataHandleTask() {
        log.info("定时处理鸿图主动推送的数据");

        List<HtPushData> list = service.list(new LambdaQueryWrapper<HtPushData>()
                .eq(HtPushData :: getStatus , 0)
                .orderByAsc(HtPushData :: getCreateTime)
                .last(" limit 100"));

        for (HtPushData data : list){
            publisher.pushEvent(data);
        }

    }


    @Override
    public void action(String jobParams,String taskId) {
        htPushDataHandleTask();
    }

    @Override
    public void executeXxlJob() throws InterruptedException {
        htPushDataHandleTask();
    }
}
